﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using Prism.Commands;
using BluetoothMachine.GlobalStatics;
using BluetoothMachine.ExpandHelper;
using BluetoothMachine.Domains;
using System.IO;
using NPOI.SS.UserModel;
using BluetoothMachine.Models;
using NPOI.XSSF.UserModel;
using BluetoothMachine.Entity;

namespace BluetoothMachine.ViewModels
{
    public class RemoteControlCoordinatePairingViewModel
    {
        private int _selectedStation;
        /// <summary>
        /// 工位
        /// </summary>
        public int SelectedStation { 
            get => _selectedStation; 
            set {
                ParameterGlobal?.SelectRCCItems(_selectedStation, value);
                _selectedStation = value; 
            } 
        }
        #region 添加坐标
        public string Name { get; set; }
        public double RCCX { get; set; }
        public double RCCY { get; set; }
        public double RCCZ { get; set; }
        #endregion
        #region 机构
        public int IntervalTime { get; set; }
        public double ACSX { get; set; }
        public double ACSY { get; set; }
        public double ACSZ { get; set; }
        #endregion
        public ParameterConfigurationGlobal ParameterGlobal { get; set; }
        private HomeGlobal _homeGlobal;

        public RemoteControlCoordinatePairingViewModel(ParameterConfigurationGlobal parameter, HomeGlobal homeGlobal)
        {
            SelectedStation = 0;
            ParameterGlobal = parameter;
            IntervalTime = 2;
            _homeGlobal = homeGlobal;
        }

        public DelegateCommand AddItem => new DelegateCommand(() =>
        {
            if (ParameterGlobal.RCCItems.Count(x => x.KeyName == Name) > 0) return;
            ParameterGlobal.RCCItems.Add(new RemoteControlCoordinatesModel()
            {
                KeyName = Name,
                Rccx = RCCX,
                Rccy = RCCY,
                Rccz = RCCZ,
            });
        });

        public DelegateCommand SaveData => new DelegateCommand(() =>
        {
            //ParameterGlobal.SaveRCC(SelectedStation);
        });

        public DelegateCommand InputData => new DelegateCommand(() =>
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "文件(*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.ValidateNames = false;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Multiselect = false;//允许同时选择多个文件
            if (openFileDialog.ShowDialog() == true) {
                using var stream = new FileStream(openFileDialog.FileName, FileMode.Open);
                IWorkbook wb = new XSSFWorkbook(stream);
                ISheet sheet = wb.GetSheetAt(0);
                RemoteControlCoordinatesModel model = new RemoteControlCoordinatesModel();
                for (int r = 1; r < sheet.LastRowNum; r++)
                {
                    IRow currentRow = sheet.GetRow(r);
                    //for (int c = 0; c < currentRow.LastCellNum; c++)
                    //{
                    //    ICell cell = currentRow.GetCell(c);
                    //    if (cell != null)
                    //    {
                    //        switch (c)
                    //        {
                    //            case 0: model.Name = cell.StringCellValue; break;
                    //            case 1: model.RCCX = cell.NumericCellValue; break;
                    //            case 2: model.RCCY = cell.NumericCellValue; break;
                    //            case 3: model.RCCZ = cell.NumericCellValue; break;
                    //        }
                    //    }
                    //}
                    //new RemoteControlCoordinates()
                    //{
                    //    KeyName = model.Name,
                    //    Rccx = model.RCCX,
                    //    Rccy = model.RCCY,
                    //    Rccz = model.RCCZ,
                    //    ProductModel = ParameterGlobal.ParameterModel.ProductModel
                    //}.Insert();
                    //ParameterGlobal.RCCItems.Add(new RemoteControlCoordinatesModel()
                    //{
                    //    Name = model.Name,
                    //    RCCX = model.RCCX,
                    //    RCCY = model.RCCY,
                    //    RCCZ = model.RCCZ,
                    //});
                }
                wb.Close();
            }
        });

        /// <summary>
        /// 执行
        /// </summary>
        public DelegateCommand Perform => new DelegateCommand(() =>
        {
            TaskManagers.GetObject.Publish(new Services.Models.KeyTestModel()
            {
                Station = new ushort[] { 1, 2 },
                XYZ = new double[] { ACSX, ACSY, ACSZ },
                RCCX = 0,
                RCCY = 1,
                RCCZ = 2,
                IntervalTime = IntervalTime,
                CardID = new ushort[] { LTDMC.CardID[0]},
            });
        });

        /// <summary>
        /// 急停
        /// </summary>
        public DelegateCommand Scram => new DelegateCommand(() =>
        {
            _homeGlobal.ScramToken.Cancel();
            LTDMC.dmc_emg_stop(LTDMC.CardID[0]);
            LTDMC.dmc_emg_stop(LTDMC.CardID[1]);
        });
    }
}
